package com.park.web.service;

import com.park.web.dao.UserDAO;
import com.park.web.entity.User;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
@Service
public class UserService {
    @Autowired
    UserDAO userDAO;

    public boolean isExist(String username) {
        User user = getByName(username);
        return null!=user;
    }

    public User getByName(String username) {
        return userDAO.findByUsername(username);
    }

    public User get(String username, String password){
        return userDAO.getByUsernameAndPassword(username, password);
    }

    public void add(User user) {
        userDAO.save(user);
    }
    public int delete(int id) {
        boolean isExist=userDAO.existsById(id);
        if (isExist) {
            userDAO.deleteById(id);
            return id;
        }
        return 0;
    }

    public int update(User user){
        if(null!=user){
            int id=user.getId();
            boolean isExist=userDAO.existsById(id);
            if(isExist){
                userDAO.save(user);
                return  id;
            }
        }
        return 0;
    }
}
